package com.eascs.crm.common.config;

import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.eascs.ScanIndex;
import com.eascs.crm.web.intercepter.AccessInterceptorAdapter;
import com.eascs.tag.MenuTag;
import com.eascs.tag.PermissionTag;
import com.eascs.web.DefaultWebConfig;
import com.eascs.web.sso.SsoUserInterceptor;

@Configuration
@EnableWebMvc
@EnableTransactionManagement(proxyTargetClass = true)
// @EnableAspectJAutoProxy
@ComponentScan(basePackageClasses = { ScanIndex.class })
@EnableScheduling
public class WebConfig extends DefaultWebConfig {

	@Bean
	public CommonsMultipartResolver multipartResolver() {
		return new org.springframework.web.multipart.commons.CommonsMultipartResolver();
	}

	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("forward:/customer/getCustomerBasicInfo");
	}

	// 添加自定义拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
		registry.addInterceptor(ssoUserInterceptor());
		registry.addInterceptor(accessInterceptorAdapter());
	}
	
	@Bean
	public HandlerInterceptorAdapter accessInterceptorAdapter() {
		return new AccessInterceptorAdapter();
	}

	@Bean
	public HandlerInterceptorAdapter ssoUserInterceptor() {
		return new SsoUserInterceptor();
	}

	@Override
	protected Map<String, Object> buildFreemarkerVariables() {
		Map<String, Object> variables = super.buildFreemarkerVariables();
		variables.put("permission", new PermissionTag());
		variables.put("menu", new MenuTag());
		return variables;
	}
}
